<?php
namespace app\serverarticle\service\article;

use \app\manage\service\ApiBase;
use think\Validate;
/**
 * 文章分类类
 * @author  蒋哲
 * 验证：
        添加或编辑时：
        [不需要验证的字段:''默认值]   [需要验证但不require的:' '默认,使其验证]   [必须require的字段:无需默认值]
        搜索时：
        可空，给予''默认值，跳过验证器即搜索值为空时，不验证，不搜索
 * 编辑类，传什么验什么
 */
class ApiArticleClassify extends ApiBase
{
    protected $addFields   = ["name"=>" ", "key"=>" ", "pid"=>0, "order"=>0,"description"=>" ","status"=>"1","add_user_id"=>""];
    protected $articleValidate = 'app\serverarticle\validate\ArticleClassify';
    protected $articleLogic    = '\app\serverarticle\logic\ArticleClassify';

    // 文章分类添加
    public function add()
    {
        if (empty($this->arrData)) {
            return null_data($this->arrData,"参数不能为空！");
        }
        $data = $this->receiveData($this->addFields);
        return $this->stepRun($data,'classifyAdd','add',$this->articleValidate,$this->articleLogic);
    }

    // 文章分类编辑
    public function edit(): array
    {
        if (empty($this->arrData)) {
            return null_data($this->arrData,"参数不能为空！");
        }
        $default = array_merge($this->addFields,['id'=>""]);
        $data = $this->issetReceiveData($default);
        return $this->stepRun($data,'classifyEdit','edit',$this->articleValidate,$this->articleLogic);
    }

    // 启用某个分类文章分类
    public function classifyEnable()
    {
        if (empty($this->arrData)) {
            return null_data($this->arrData,"参数不能为空！");
        }
        $data = $this->receiveData(['id'=>'','key'=>'']);
        return $this->stepRun($data,'classifyEnable','enable',$this->articleValidate,$this->articleLogic);
    }

    // 禁用某个分类文章分类
    public function classifyDisable()
    {
        if (empty($this->arrData)) {
            return null_data($this->arrData,"参数不能为空！");
        }
        $data = $this->receiveData(['id'=>'','key'=>'']);
        return $this->stepRun($data,'classifyDisable','enable',$this->articleValidate,$this->articleLogic);
    }

    // 删除某个分类文章分类
    public function classifyDelete()
    {
        if (empty($this->arrData)) {
            return null_data($this->arrData,"参数不能为空！");
        }
        $data = $this->receiveData(['id'=>'','key'=>'']);
        return $this->stepRun($data,'classifyDelete','enable',$this->articleValidate,$this->articleLogic);
    }

    // 全部文章分类列表获取
    public function classifyList()
    {
        return $this->logic($this->articleLogic)->classifyList();
    }

    // 某一类文章分类列表获取
    public function classifySomeList()
    {
        if (empty($this->arrData)) {
            return null_data($this->arrData,"参数不能为空！");
        }
        $data = $this->receiveData(['pid'=>'','key'=>'']);
        return $this->stepRun($data,'classifySomeList','pid',$this->articleValidate,$this->articleLogic);
    }

    // 不同条件文章分类列表获取 (分页)
    public function classifyCondition()
    {
        if (empty($this->arrData)) {
            return null_data($this->arrData,"参数不能为空！");
        }
        $data = $this->receiveData(['keywords'=>'','listRows'=>15,'page'=>1]);
        // keywords非必须，不验证
        return $this->logic($this->articleLogic)->ClassifyCondition($data);
    }
}
